﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CardLogistics.Model.CardMaking;
using System.Data.OracleClient;

namespace CardLogistics.DAL.CardMaking
{
    /// <summary>
    /// 卡片制作_卡片需求单数据访问类
    /// </summary>
    public class CardMaking_CardDemandData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_CardMaking_CardDemand", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CardMaking_CardDemand GetModel(string rId)
        {
            CardMaking_CardDemand model = new CardMaking_CardDemand();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["DEMANDNO"] != null && row["DEMANDNO"].ToString() != "")
                {
                    model.DEMANDNO = row["DEMANDNO"].ToString();
                }
                if (row["APPLICANT"] != null && row["APPLICANT"].ToString() != "")
                {
                    model.APPLICANT = row["APPLICANT"].ToString();
                }
                if (row["CARDMECHANISM"] != null && row["CARDMECHANISM"].ToString() != "")
                {
                    model.CARDMECHANISM = row["CARDMECHANISM"].ToString();
                }
                if (row["APPLICADATE"] != null && row["APPLICADATE"].ToString() != "")
                {
                    model.APPLICADATE = DateTime.Parse(row["APPLICADATE"].ToString());
                }
                if (row["CARDDATE"] != null && row["CARDDATE"].ToString() != "")
                {
                    model.CARDDATE = DateTime.Parse(row["CARDDATE"].ToString());
                }
                if (row["REMARKS"] != null && row["REMARKS"].ToString() != "")
                {
                    model.REMARKS = row["REMARKS"].ToString();
                }
                if (row["LASTUPDATEUSER"] != null && row["LASTUPDATEUSER"].ToString() != "")
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["ADDTIME"] != null && row["ADDTIME"].ToString() != "")
                {
                    model.ADDTIME = DateTime.Parse(row["ADDTIME"].ToString());
                }
                if (row["ISSUBMIT"] != null && row["ISSUBMIT"].ToString() != "")
                {
                    model.ISSUBMIT = decimal.Parse(row["ISSUBMIT"].ToString());
                }

                #region 添加便于查询的字段
                
                if (row["SalesName"] != null && row["SalesName"].ToString() != "")
                {
                    model.SalesName = row["SalesName"].ToString();//用卡机构名称
                }
                if (row["CardTotal"] != null && row["CardTotal"].ToString() != "")
                {
                    model.CardTotal = Convert.ToInt32(row["CardTotal"]);//卡片需求总数
                }
                #endregion
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(CardMaking_CardDemand model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("v_varOut", OracleType.VarChar,50),
					new OracleParameter("V_DEMANDNO", OracleType.NVarChar),
					new OracleParameter("V_APPLICANT", OracleType.NVarChar),
					new OracleParameter("V_CARDMECHANISM", OracleType.NVarChar),
					new OracleParameter("V_APPLICADATE", OracleType.DateTime),
					new OracleParameter("V_CARDDATE", OracleType.DateTime),
					new OracleParameter("V_REMARKS", OracleType.NVarChar),
					new OracleParameter("V_LASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("V_LASTUPDATEDATE", OracleType.DateTime),
					new OracleParameter("V_ADDTIME", OracleType.DateTime),
					new OracleParameter("V_ISSUBMIT", OracleType.Number,4),
                    new OracleParameter("v_details", OracleType.VarChar,2000)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.DEMANDNO;
                parameters[2].Value = model.APPLICANT;
                parameters[3].Value = model.CARDMECHANISM;
                parameters[4].Value = model.APPLICADATE;
                parameters[5].Value = model.CARDDATE;
                parameters[6].Value = model.REMARKS;
                parameters[7].Value = model.LASTUPDATEUSER;
                parameters[8].Value = model.LASTUPDATEDATE;
                parameters[9].Value = model.ADDTIME;
                parameters[10].Value = model.ISSUBMIT;
                parameters[11].Value = model.Details;

                string StrOutPar = DbHelperSQL.RunProcedure("P_CardMaking_CardDemand_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(CardMaking_CardDemand model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("V_ID", OracleType.NVarChar),
					new OracleParameter("V_DEMANDNO", OracleType.NVarChar),
					new OracleParameter("V_APPLICANT", OracleType.NVarChar),
					new OracleParameter("V_CARDMECHANISM", OracleType.NVarChar),
					new OracleParameter("V_APPLICADATE", OracleType.DateTime),
					new OracleParameter("V_CARDDATE", OracleType.DateTime),
					new OracleParameter("V_REMARKS", OracleType.NVarChar),
					new OracleParameter("V_LASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("V_LASTUPDATEDATE", OracleType.DateTime),
					new OracleParameter("V_ADDTIME", OracleType.DateTime),
					new OracleParameter("V_ISSUBMIT", OracleType.Number,4),
                    new OracleParameter("v_details", OracleType.VarChar,2000),
                    new OracleParameter("v_varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.DEMANDNO;
                parameters[2].Value = model.APPLICANT;
                parameters[3].Value = model.CARDMECHANISM;
                parameters[4].Value = model.APPLICADATE;
                parameters[5].Value = model.CARDDATE;
                parameters[6].Value = model.REMARKS;
                parameters[7].Value = model.LASTUPDATEUSER;
                parameters[8].Value = model.LASTUPDATEDATE;
                parameters[9].Value = model.ADDTIME;
                parameters[10].Value = model.ISSUBMIT;
                parameters[11].Value = model.Details;
                parameters[12].Direction = ParameterDirection.Output;

                string StrOutPar = DbHelperSQL.RunProcedure("P_CardMaking_CardDemand_UPDATE", parameters, parameters[12]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息(同时删除需求单明细)
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("v_id",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_CardMaking_CardDemand_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
